// fillings for division header
#include "Includes.hpp" // includes for project
#include "Division.hpp" // class declarations

class Team;

////////////////////////////// CONSTRUCTORS //////////////////////////
Division::Division() { // this should not happen
    cout << "\n\tyou are doing it wrong\n\tsee division class\n"; // print error
}

Division::Division(ifstream* in, char* sentname) : Text_element(sentname) {
    in->ignore(2, '#'); // ignore delimiter
    (*in) >> teamcount; // read 'teamcount'
    (*in).ignore(2, '#'); // ignore leftover crlf
    if (teamcount > 0 && teamcount <= MAXTEAMS){
	for (int tvar = 0; tvar <= teamcount; tvar++) { // go through all teams
	    teamsarr[tvar] = new Team(in); // read new team from file-stream
	} // for tvar end
	for (int svard = 0; svard <= teamcount+1; svard++) { // go through schedule downways,
	    in->ignore(2, '\n'); // ignore delimiter         // +1 for the initial line
	    for (int svars = 0; svars <= teamcount; svars++) { // go through schedule sideways
		schedule[svard][svars] = new Result(in); // read itself from file
	    } // for svars end
	} // for svard end
    } else if (teamcount > MAXTEAMS) {
	cout << "\n\tthere are to many teams in file (Division::Division(ifstream*,char*))"; // print error
    } else {
	cout << "\n\tthere are no teams in file (Division::Division(ifstream*,char*))"; // print error
    }
} // function end

Division::Division(char * sentname) : Text_element(sentname) {
    teamcount = 0; // initialize number of teams
    for (int tvar = 0; tvar <= MAXTEAMS; tvar++) { // go through memory of 'teamsarr[]'
	teamsarr[tvar] = NULL; // and set them all to NULL
    } // for tvar end
    for (int svars = 0; svars <= MAXTEAMS; svars++) { // go through memory of 'schedule[][]'
	for (int svard = 0; svard <= MAXTEAMS; svard++) { // and set all of them
	    schedule[svars][svard] = NULL; // to NULL
	} // for svard end
    } // for svars end
}

/////////////////////////// CLASS FUNCTIONS //////////////////////////
void Division::display() {
	cout << "\n" << text << "\nhas " << teamcount << " number of teams\n";
	for (int tvar = 0; tvar <= teamcount; tvar++) {
		teamsarr[tvar]->display();
	}
	printSchedule();
}

void Division::writeToFile(ofstream* out) {
    (*out) << text << " #" // write division name
	   << teamcount << " #"; // write current number of teams
    for (int tvar = 0; tvar <= teamcount; tvar++) { // for all teams
	(*teamsarr[tvar]).writeToFile(out); // write itself to file
    } // for tvar end
    for (int svard = 0; svard <= teamcount+1; svard++) { // go through schedule downways
	(*out) << endl; // print newline                   // +1 for the initial line
	for (int svars = 0; svars <= teamcount; svars++) { // go through schedule sideways
	    (*schedule[svard][svars]).writeToFile(out); // write itself to file
	} // for svars end
    } // for svard end
}

void Division::printSchedule() {
    for (int svard = 0; svard <= teamcount; svard++) { // go through schedule downways
	for (int svars = 0; svars <= teamcount; svars++) { // go through schedule sideways
	    cout << (*schedule[svard][svars]).date << ' ' << (*schedule[svard][svars]).homegoals
		 << ' ' << (*schedule[svard][svars]).awaygoals // prints schedule to screen
		 << ' ' << (*schedule[svard][svars]).normalplaytime;
	    for (int hsvar = 0; hsvar <= (*schedule[svard][svars]).homegoals; hsvar++) {
		cout << (*schedule[svard][svars]).homescorers[hsvar] << ' ';
	    }
	    for (int asvar = 0; asvar <= (*schedule[svard][svars]).awaygoals; asvar++) {
		cout << (*schedule[svard][svars]).awayscorers[asvar] << ' ';
	    }
	} // for svars end
	cout << endl;
    } // for svard end
}

void Division::writeSchedule(ofstream* out) {
    for (int svard = 0; svard <= teamcount; svard++) { // go through schedule downways
		for (int svars = 0; svars <= teamcount; svars++) { // go through schedule sideways
			(*schedule[svard][svars]).writeToFile(out);
		} // for svars end
		(*out) << endl;
    } // for svard end
}

void Division::printWriteTable(ostream* out, int pointtype) {
	Table* tables[MAXTEAMS]; // the entire table, is a table of tables
	for (int tvar = 0; tvar <= teamcount; tvar++) { // go through all the current ones
		tables[tvar] = new Table(teamsarr[tvar]); // make new tables
	} // for tvar end
	for (int stvar = 0; stvar <= teamcount; stvar++) {
		for (int stvart = 0; stvart <= teamcount; stvart++) {
			if (stvar != stvart) {
				schedule[stvar][stvart]->makeTable(tables[stvar], tables[stvart], pointtype); // make a table
			} // if stvar != stvart end
		} // for stvart end
	} // for stvar end
	selectionSortTable(*tables); // sort the tables
	(*out) << "\nTeam\tP\tW\tT\tL\tG\tP\n";
	for (int pvar = 0; pvar <= teamcount; pvar++) {
		(*out) << tables[pvar]->team->getName() << '\t'
			<< tables[pvar]->played << '\t'
			<< tables[pvar]->won << '\t'
			<< tables[pvar]->tied << '\t'
			<< tables[pvar]->lost << '\t'
			<< tables[pvar]->goals << '\t'
			<< tables[pvar]->points << '\n';
	}
}
